package com.mobilepay.moneypig.model.activity.mine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.ShareData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.QRCodeUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by xlm on 2017/7/25 0025
 */

public class MineQRCodeActivity extends BaseActivity {
    private static final String TAG = "MineQRCodeActivity";
    private RelativeLayout mRelativeBack;
    private ImageView mImageCode;
    private TextView mTextShare;
    private TextView mTextInvite;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_code);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.code_back);
        mImageCode = (ImageView) findViewById(R.id.code_image);
        mTextShare = (TextView) findViewById(R.id.code_share);
        mTextInvite = (TextView) findViewById(R.id.code_invite);
    }

    Bitmap bitmap;
    String urlshare;

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        String inviteCode = App.getApp().getUserInfo().getData().getInvitation_code();
        urlshare = App.getApp().getUserDetailInfo().getData().getInvitation_url();//是否为有效的url地址
        bitmap = QRCodeUtil.createQRImage(urlshare);//生成的二维码
        mImageCode.setImageBitmap(bitmap);
        mTextInvite.setText("邀请码：" + inviteCode);
        getShareData();
    }

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(this);
        mTextShare.setOnClickListener(this);
    }

    String title = "0投入，立刻赚钱只需3步";
    String content = "微信、支付宝、银联快捷收款实时到帐。" +
            "注册下载免费拥有，分享推广即可赚钱";
    String url = "";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.code_back:
                finish();
                break;
            case R.id.code_share:
                new ShareAction(getThis())
                        .withMedia(new UMWeb(urlshare,
                                        title,
                                        content,
                                        CommonUtil.isEmpty(url) ?
                                                new UMImage(getThis(),
                                                        R.mipmap.pig_icon) :
                                                new UMImage(getThis(), url)
                                )
                        )
                        .setDisplayList(SHARE_MEDIA.QQ,
                                SHARE_MEDIA.QZONE,
                                SHARE_MEDIA.WEIXIN,
                                SHARE_MEDIA.WEIXIN_CIRCLE,
                                SHARE_MEDIA.ALIPAY,
                                SHARE_MEDIA.SMS,
                                SHARE_MEDIA.SINA)
                        .open(App.setShareBoard());
                break;
        }
    }

    private void getShareData() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.share_data);
        param.add("uid", String.valueOf(App.getApp().getUserDetailInfo().getData().getUid()));
        param.add("group", "share");
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executeGet(new HttpUtils.HttpListener() {
            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        ShareData data = GsonUtil.GsonToBean(string, ShareData.class);
                        title = data.getData().getShare_title();
                        content = data.getData().getShare_text();
                        url = data.getData().getShare_cover();
                    }
                }
            }

            @Override
            public void onException(Throwable e) {
                super.onException(e);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

}
